package com.example.vdmtest;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.vivo.ic.dm.Constants;
import com.vivo.ic.dm.DMUtil;
import com.vivo.ic.dm.DownloadManager;
import com.vivo.ic.dm.Downloads;

import java.util.ArrayList;

public class MainTestAdapter extends BaseAdapter implements View.OnClickListener {
    private static final String TAG = MainApplication.PRE_TAG + "MainTestAdapter";
    private Context mContext;
    private ArrayList<MainTestItem> mItems;
    private DownloadManager mDownloadManager;
    private TestAdapterCallBack mCallBack;

    public MainTestAdapter(Context context, ArrayList<MainTestItem> items, TestAdapterCallBack callback) {
        mContext = context;
        mItems = items;
        mDownloadManager = DownloadManager.getInstance();
        mCallBack = callback;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public MainTestItem getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View view, ViewGroup parent) {


        View rowView = view;
        ViewHolder holder;
        MainTestItem item = mItems.get(position);
        Log.d(TAG, "getView() item:" + item);
        if (rowView != null) {
            holder = (ViewHolder) rowView.getTag();

        } else {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            rowView = inflater.inflate(R.layout.item, null);
            holder.title = (TextView) rowView.findViewById(R.id.title);
            holder.progress = (ProgressBar) rowView.findViewById(R.id.progress);
            holder.percent = (TextView) rowView.findViewById(R.id.percent);
            holder.speed = (TextView) rowView.findViewById(R.id.speed);
            holder.control = (TextView) rowView.findViewById(R.id.control);
            holder.cancel = (TextView) rowView.findViewById(R.id.cancel);

        }

        // 适配界面
        holder.title.setText(item.title);

        updateView(item, holder);

        holder.control.setOnClickListener(this);
        holder.control.setTag(item);
        holder.cancel.setOnClickListener(this);
        holder.cancel.setTag(item);

        rowView.setTag(holder);

        return rowView;
    }

    public void updateView(MainTestItem item, ViewHolder holder) {
        Log.d(TAG, "updateView MainTestItem: " + item.toString());
        if (item.percent > 0) {
            holder.percent.setText(item.percent + "%");
        } else if (!TextUtils.isEmpty(item.tip)) {
            holder.percent.setText(item.tip);
        } else {
            holder.percent.setText("percent");
        }

        if (item.speed >= 0) {
            holder.speed.setText(item.speed + " b/s");
        } else {
            holder.speed.setText("speed");
        }

        if (Downloads.Impl.isStatusCompleted(item.status)) {
            holder.control.setText("结束");
            holder.progress.setProgress(item.percent);
        } else if (Downloads.Impl.CONTROL_PAUSED == item.control) {
            holder.control.setText("继续");
            holder.progress.setProgress(item.percent);
        } else if (item.status > 0 && !Downloads.Impl.isStatusError(item.status)) {
            holder.control.setText("暂停");
            holder.progress.setProgress(item.percent);
        } else {
            holder.control.setText("下载");
            holder.progress.setProgress(item.percent);
        }
    }

    public static class ViewHolder {
        TextView title;
        ProgressBar progress;
        TextView percent;
        TextView speed;

        TextView control;
        TextView cancel;
    }

    @Override
    public void onClick(final View v) {
        Log.d(TAG, "onClick control or cancel");
        switch (v.getId()) {
        case R.id.control:
            MainTestItem controlItem = (MainTestItem) v.getTag();
            int status = controlItem.status;
            int control = controlItem.control;
            Log.d(TAG, "onClick status: " + status);
            if (Downloads.Impl.isStatusCompleted(status)) {
                // 完成
                Toast.makeText(mContext, "已结束的下载", Toast.LENGTH_SHORT).show();

            } else if (Downloads.Impl.CONTROL_PAUSED == control) {
                // 继续
                Log.d(TAG, "onClick go on");
                if (DMUtil.getNetValueNow() == Constants.CHANGE_STATE_NONE) {
                    Toast.makeText(mContext, R.string.dm_noti_wlan_disconnected, Toast.LENGTH_LONG).show();
                } else {
                    mDownloadManager.continueDownloadInQueue(Downloads.Column.URI + "=?",
                            new String[]{controlItem.url});
                }


            } else if (status > 0) {
                // 暂停
                Log.d(TAG, "onClick pause");
                mDownloadManager.pauseDownloadInqueue(Downloads.Column.URI + "=?",
                        new String[]{controlItem.url});

            } else {
                // 下载
                Log.d(TAG, "onClick begin download");
                if (DMUtil.getNetValueNow() == Constants.CHANGE_STATE_NONE) {
                    Toast.makeText(mContext, R.string.dm_noti_wlan_disconnected, Toast.LENGTH_LONG).show();
                } else {
                    ContentValues values = new ContentValues();
                    values.put(Downloads.Column.URI, controlItem.url);
                    values.put(Downloads.Column.TITLE, controlItem.title);
                    mDownloadManager.startDownloadInquene(values);
                }

            }
            v.setEnabled(false);
            Log.d(TAG,"control btn isEnabled 1: " + v.isEnabled());
            v.postDelayed(new Runnable() {
                @Override
                public void run() {
                    v.setEnabled(true);
                    Log.d(TAG,"control btn isEnabled 2: " + v.isEnabled());
                }
            }, 5000);
            mCallBack.onChange();
            break;

        case R.id.cancel:
            MainTestItem cancelItem = (MainTestItem) v.getTag();
            mDownloadManager.cancelDownloadInQueue(Downloads.Column.URI + "=?", new String[]{cancelItem.url});
            mCallBack.onChange();
            break;

        default:
            break;
        }

    }

    interface TestAdapterCallBack {
        void onChange();
    }
}
